//
//  ParentViewController.swift
//  导航切换
//
//  Created by wanjiading on 2016/12/31.
//  Copyright © 2016年 wjd. All rights reserved.
//

import UIKit

class ParentViewController: UIViewController {

    @IBOutlet weak var scrollView: UIScrollView!
    override func viewDidLoad() {
        super.viewDidLoad()

        automaticallyAdjustsScrollViewInsets = false

        let vc1 = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "FirstChild")
        let vc2 = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "SecondChild")

        self.addChildViewController(vc1)
        self.addChildViewController(vc2)

        self.scrollView.addSubview(vc1.view)
        self.scrollView.addSubview(vc2.view)

        let size = UIScreen.main.bounds.size
        let topMargin: CGFloat = 64
        let scrollViewHeight = size.height - topMargin

        vc1.view.frame = CGRect(x: 0, y: 0, width: size.width, height: scrollViewHeight)
        vc2.view.frame = CGRect(x: size.width, y: 0, width: size.width, height: scrollViewHeight)

        scrollView.isPagingEnabled = true
        scrollView.contentSize = CGSize(width: size.width * 2, height: scrollViewHeight)
    }

    @IBAction func firstBtnClick(_ sender: UIButton) {
        scrollView.setContentOffset(CGPoint.zero, animated: true)
    }
    
    @IBAction func secondBtnClick(_ sender: UIButton) {
        scrollView.setContentOffset(CGPoint(x: view.frame.size.width, y: 0), animated: true)
    }
}
